# coding=utf-8
from rest_framework.views import APIView
from rest_framework.response import Response
from fe_api.sutra.utils import viewutils
from fe_api.sutra import forms


class SutraEditionSearch(APIView):
    '''
    sutras search
    '''

    def get(self, request):
        form = forms.SutraEditionSearch(request.query_params)
        if not form.is_valid():
            return Response({
                "status": 400,
                "msg": "parameter error",
            })
        rdata = form.cleaned_data
        user = request.user
        data = viewutils.sutras_edition_search(
            user, rdata['name'],
            rdata['offset'], rdata['limit'])
        return Response({
            'status': 200,
            'msg': 'success',
            'data': data})


class Sutras(APIView):
    '''
    sutras list
    '''

    def get(self, request):
        form = forms.Sutras(request.query_params)
        if not form.is_valid():
            return Response({
                "status": 400,
                "msg": "parameter error",
            })
        rdata = form.cleaned_data
        data = viewutils.sutras_list(
            rdata['offset'], rdata['limit'])
        return Response({
            'status': 200,
            'msg': 'success',
            'data': data})


class SutrasDetail(APIView):
    '''
    sutras detail
    '''

    def get(self, request, sutra_id=None):
        if not sutra_id:
            return Response({'status': 400})
        data = viewutils.sutras_detail(sutra_id)
        return Response({
            'status': 200,
            'msg': 'success',
            'data': data})


class SutraEdition(APIView):
    '''
    edition list
    '''

    def get(self, request, sutra_id=None):
        form = forms.SutraEdition(request.query_params)
        if not form.is_valid() or not sutra_id:
            return Response({
                "status": 400,
                "msg": "parameter error",
            })
        rdata = form.cleaned_data
        data = viewutils.sutras_edition_list(
            sutra_id, rdata['offset'], rdata['limit'])
        return Response({
            'status': 200,
            'msg': 'success_ss',
            'data': data})


class SutraEditionDetail(APIView):
    '''
    edition detail
    '''

    def get(self, request, edition_id=None):
        if not edition_id:
            return Response({'status': 400})
        data = viewutils.sutras_edition_detail(
            edition_id)
        return Response({
            'status': 200,
            'msg': 'success',
            'data': data})


class SutraEditionContent(APIView):
    '''
    edition content
    '''

    def get(self, request, edition_id=None):
        if not edition_id:
            return Response({'status': 400})
        page_num = request.query_params.get('page_num', 1)
        data = viewutils.sutras_edition_content(
            edition_id, int(page_num))
        return Response({
            'status': 200,
            'msg': 'success',
            'data': data})


class SutraEditionSearchHotwords(APIView):

    def get(self, request):
        """
        获取搜索热词
        """
        hot_words = viewutils.get_search_hotwords()
        return Response({
            "status": 200,
            "msg": "success",
            "data": {
                "hot_words": hot_words,
            }
        })
